Click START to begin real-time note recognition from your microphone.
\"),\n", " status_label,\n", " button_box\n", " ])\n", " \n", " display(interface)\n", " \n", " return recognizer\n", "\n", "# Test microphone availability\n", "def test_microphone():\n", " \"\"\"Test if microphone is available and working\"\"\"\n", " print(\"Testing microphone availability...\")\n", " \n", " try:\n", " # List available audio devices\n", " print(\"\\nAvailable audio devices:\")\n", " print(sd.query_devices())\n", " \n", " print(f\"\\nDefault input device: {sd.query_devices(kind='input')}\")\n", " \n", " # Test recording\n", " print(\"\\nTesting microphone (recording 2 seconds)...\")\n", " duration = 2.0\n", " sample_rate = 22050\n", " \n", " recording = sd.rec(\n", " int(duration * sample_rate), \n", " samplerate=sample_rate, \n", " channels=1\n", " )\n", " sd.wait() # Wait until recording is finished\n", " \n", " max_amplitude = np.max(np.abs(recording))\n", " print(f\"Max amplitude recorded: {max_amplitude:.4f}\")\n", " \n", " if max_amplitude > 0.001:\n", " print(\"β Microphone is working!\")\n", " \n", " # Play back the recording\n", " print(\"Playing back your recording...\")\n", " display(ipd.Audio(recording.flatten(), rate=sample_rate))\n", " \n", " return True\n", " else:\n", " print(\"β οΈ No sound detected. Check your microphone.\")\n", " return False\n", " \n", " except Exception as e:\n", " print(f\"β Microphone test failed: {e}\")\n", " print(\"Make sure you have a microphone connected and permissions enabled.\")\n", " return False\n", "\n", "print(\"Microphone recognition system loaded!\")\n", "print(\"\\nUsage:\")\n", "print(\"1. test_microphone() # Test your microphone\")\n", "print(\"2. create_microphone_interface() # Start real-time recognition\")" ] }, { "cell_type": "code", "execution_count": 13, "id": "a2e04aaf", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "β sounddevice already installed\n", "β ipywidgets already installed\n" ] }, { "data": { "application/javascript": "IPython.notebook.kernel.execute('jupyter nbextension enable --py widgetsnbextension')", "text/plain": [ "Click START to begiβ¦" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "<__main__.RealTimeNoteRecognizer at 0x1699f4740>" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "create_microphone_interface()" ] }, { "cell_type": "code", "execution_count": 27, "id": "9a54b572", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Fixed recording functions loaded!\n", "\n", "Usage:\n", "1. test_microphone_simple() # Test microphone first\n", "2. record_5_seconds_fixed() # Record and analyze\n" ] } ], "source": [ "# Modified Real-time Microphone Note Recognition with 5-second timeout\n", "def create_microphone_interface_5sec():\n", " \"\"\"Create interactive microphone recognition interface with 5-second auto-stop\"\"\"\n", " \n", " # Check if models are available\n", " if 'trained_models_multi' not in locals() and 'trained_models_multi' not in globals():\n", " print(\"β Models not found! Please run the training cells first.\")\n", " return\n", " \n", " # Create recognizer\n", " recognizer = RealTimeNoteRecognizer(\n", " trained_models_multi, \n", " scaler_multi, \n", " label_encoder, \n", " extract_comprehensive_features\n", " )\n", " \n", " # Create interface buttons\n", " start_button = widgets.Button(\n", " description='π€ START 5-SEC RECORDING',\n", " button_style='success',\n", " layout=widgets.Layout(width='250px')\n", " )\n", " \n", " stop_button = widgets.Button(\n", " description='βΉοΈ STOP RECORDING', \n", " button_style='danger',\n", " layout=widgets.Layout(width='200px')\n", " )\n", " \n", " status_label = widgets.HTML(value=\"Status: Ready to start 5-second recording\")\n", " countdown_label = widgets.HTML(value=\"\")\n", " \n", " # Timer variables\n", " timer_thread = None\n", " \n", " def countdown_timer():\n", " \"\"\"5-second countdown timer\"\"\"\n", " for i in range(5, 0, -1):\n", " if not recognizer.is_recording:\n", " break\n", " countdown_label.value = f\"Time remaining: {i} seconds\"\n", " time.sleep(1)\n", " \n", " # Auto-stop after 5 seconds\n", " if recognizer.is_recording:\n", " recognizer.stop_recording()\n", " status_label.value = \"Status: βΈοΈ Auto-stopped after 5 seconds\"\n", " countdown_label.value = \"Recording completed!\"\n", " start_button.disabled = False\n", " stop_button.disabled = True\n", " \n", " # Show final results\n", " print(\"\\nπ― 5-SECOND RECORDING COMPLETED!\")\n", " print(\"=\" * 50)\n", " if recognizer.prediction_history:\n", " # Get most recent predictions\n", " recent_preds = list(recognizer.prediction_history)[-3:]\n", " print(\"Final predictions from last 3 chunks:\")\n", " for i, pred in enumerate(recent_preds, 1):\n", " print(f\" {i}. {pred['note']} (confidence: {pred['confidence']:.1%})\")\n", " \n", " # Most common prediction\n", " from collections import Counter\n", " all_notes = [p['note'] for p in recognizer.prediction_history]\n", " note_counts = Counter(all_notes)\n", " most_common = note_counts.most_common(1)[0]\n", " print(f\"\\nπ MOST DETECTED NOTE: {most_common[0]} ({most_common[1]} times)\")\n", " \n", " def on_start_clicked(b):\n", " try:\n", " recognizer.start_recording()\n", " status_label.value = \"Status: π΄ Recording for 5 seconds...\"\n", " countdown_label.value = \"Time remaining: 5 seconds\"\n", " start_button.disabled = True\n", " stop_button.disabled = False\n", " \n", " # Start countdown timer\n", " timer_thread = threading.Thread(target=countdown_timer, daemon=True)\n", " timer_thread.start()\n", " \n", " except Exception as e:\n", " status_label.value = f\"Status: β Error: {e}\"\n", " \n", " def on_stop_clicked(b):\n", " try:\n", " recognizer.stop_recording()\n", " status_label.value = \"Status: βΈοΈ Manually stopped\"\n", " countdown_label.value = \"Recording stopped early\"\n", " start_button.disabled = False\n", " stop_button.disabled = True\n", " clear_output(wait=True)\n", " print(\"Microphone recording stopped manually.\")\n", " except Exception as e:\n", " status_label.value = f\"Status: β Error: {e}\"\n", " \n", " start_button.on_click(on_start_clicked)\n", " stop_button.on_click(on_stop_clicked)\n", " \n", " # Initial button states\n", " stop_button.disabled = True\n", " \n", " # Create layout\n", " button_box = widgets.HBox([start_button, stop_button])\n", " interface = widgets.VBox([\n", " widgets.HTML(\"
Click START to begin 5-second note recognition from your microphone.
\"),\n", " status_label,\n", " countdown_label,\n", " button_box\n", " ])\n", " \n", " display(interface)\n", " \n", " return recognizer\n", "# Fixed 5-second recording function with better error handling\n", "import sounddevice as sd\n", "import numpy as np\n", "from collections import Counter\n", "\n", "def record_5_seconds_fixed():\n", " \"\"\"Fixed version of 5-second recording function with better error handling\"\"\"\n", " print(\"π€ Starting 5-second musical note recording...\")\n", " print(\"Make sure your microphone is connected and working!\")\n", " \n", " try:\n", " # Check if required variables exist\n", " if 'trained_models_multi' not in globals():\n", " print(\"β Error: Models not trained yet. Please run the training cells first.\")\n", " return None, None\n", " \n", " if 'scaler_multi' not in globals():\n", " print(\"β Error: Scaler not available. Please run the training cells first.\")\n", " return None, None\n", " \n", " if 'label_encoder' not in globals():\n", " print(\"β Error: Label encoder not available. Please run the training cells first.\")\n", " return None, None\n", " \n", " # Record for 5 seconds\n", " duration = 5.0\n", " sample_rate = 22050\n", " \n", " print(f\"π΄ Recording for {duration} seconds...\")\n", " print(\"Start making sounds now!\")\n", " \n", " # Start recording\n", " recording = sd.rec(\n", " int(duration * sample_rate), \n", " samplerate=sample_rate, \n", " channels=1,\n", " dtype='float64'\n", " )\n", " \n", " # Countdown\n", " import time\n", " for i in range(5, 0, -1):\n", " print(f\"β° {i}...\")\n", " time.sleep(1)\n", " \n", " # Wait for recording to finish\n", " sd.wait()\n", " print(\"β Recording complete!\")\n", " \n", " # Check if we got any audio\n", " max_amplitude = np.max(np.abs(recording))\n", " print(f\"Max amplitude recorded: {max_amplitude:.4f}\")\n", " \n", " if max_amplitude < 0.001:\n", " print(\"β οΈ Very low audio detected. Try speaking louder or check your microphone.\")\n", " \n", " # Play back the recording\n", " print(\"\\nπ Playing back your 5-second recording:\")\n", " display(ipd.Audio(recording.flatten(), rate=sample_rate))\n", " \n", " # Analyze the recording\n", " print(\"\\nπ Analyzing recording...\")\n", " audio_data = recording.flatten()\n", " \n", " # Extract features\n", " try:\n", " features = extract_comprehensive_features(audio_data, sample_rate)\n", " features_scaled = scaler_multi.transform(features.reshape(1, -1))\n", " except Exception as e:\n", " print(f\"β Feature extraction failed: {e}\")\n", " return None, None\n", " \n", " # Get predictions from all models\n", " print(\"\\nπ― Model Predictions:\")\n", " print(\"-\" * 40)\n", " \n", " predictions = {}\n", " try:\n", " for name, model in trained_models_multi.items():\n", " pred_encoded = model.predict(features_scaled)[0]\n", " pred_note = label_encoder.inverse_transform([pred_encoded])[0]\n", " predictions[name] = pred_note\n", " \n", " if hasattr(model, 'predict_proba'):\n", " proba = model.predict_proba(features_scaled)[0]\n", " confidence = proba.max()\n", " print(f\"{name:<25}: {pred_note} ({confidence:.3f})\")\n", " else:\n", " print(f\"{name:<25}: {pred_note}\")\n", " except Exception as e:\n", " print(f\"β Prediction failed: {e}\")\n", " return None, None\n", " \n", " # Ensemble result\n", " votes = list(predictions.values())\n", " vote_counts = Counter(votes)\n", " majority_vote = vote_counts.most_common(1)[0][0]\n", " vote_confidence = vote_counts.most_common(1)[0][1] / len(votes)\n", " \n", " print(f\"\\nπ FINAL RESULT:\")\n", " print(f\"Detected Note: {majority_vote}\")\n", " print(f\"Confidence: {vote_confidence:.1%} ({vote_counts.most_common(1)[0][1]}/{len(votes)} models)\")\n", " print(f\"Vote Distribution: {dict(vote_counts)}\")\n", " \n", " return majority_vote, predictions\n", " \n", " except Exception as e:\n", " print(f\"β Recording failed with error: {e}\")\n", " print(f\"Error type: {type(e).__name__}\")\n", " \n", " # Specific error handling\n", " if \"sounddevice\" in str(e).lower():\n", " print(\"π‘ Microphone issue detected. Try:\")\n", " print(\" 1. Check if your microphone is connected\")\n", " print(\" 2. Grant microphone permissions to Jupyter\")\n", " print(\" 3. Restart your kernel and try again\")\n", " elif \"trained_models_multi\" in str(e):\n", " print(\"π‘ Models not found. Please run all training cells first.\")\n", " \n", " return None, None\n", "\n", "# Test microphone first\n", "def test_microphone_simple():\n", " \"\"\"Simple microphone test\"\"\"\n", " try:\n", " print(\"Testing microphone (2 seconds)...\")\n", " duration = 2.0\n", " sample_rate = 22050\n", " \n", " recording = sd.rec(\n", " int(duration * sample_rate), \n", " samplerate=sample_rate, \n", " channels=1\n", " )\n", " sd.wait()\n", " \n", " max_amplitude = np.max(np.abs(recording))\n", " print(f\"Max amplitude: {max_amplitude:.4f}\")\n", " \n", " if max_amplitude > 0.001:\n", " print(\"β Microphone working!\")\n", " return True\n", " else:\n", " print(\"β οΈ No sound detected\")\n", " return False\n", " \n", " except Exception as e:\n", " print(f\"β Microphone test failed: {e}\")\n", " return False\n", "\n", "print(\"Fixed recording functions loaded!\")\n", "print(\"\\nUsage:\")\n", "print(\"1. test_microphone_simple() # Test microphone first\")\n", "print(\"2. record_5_seconds_fixed() # Record and analyze\")" ] }, { "cell_type": "code", "execution_count": 28, "id": "e414eae5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "π€ Starting 5-second musical note recording...\n", "Speak or play musical notes now!\n", "Recording for 5.0 seconds...\n", "||PaMacCore (AUHAL)|| Warning on line 521: err=''!obj'', msg=Unknown Error\n", "||PaMacCore (AUHAL)|| Warning on line 441: err=''!obj'', msg=Unknown Error\n", "||PaMacCore (AUHAL)|| Error on line 1322: err='-10851', msg=Audio Unit: Invalid Property Value\n", "β Recording failed: Error opening InputStream: Internal PortAudio error [PaErrorCode -9986]\n" ] }, { "data": { "text/plain": [ "(None, None)" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "record_5_seconds()" ] }, { "cell_type": "code", "execution_count": 29, "id": "0546a1e0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Testing microphone (2 seconds)...\n", "||PaMacCore (AUHAL)|| Warning on line 521: err=''!obj'', msg=Unknown Error\n", "||PaMacCore (AUHAL)|| Warning on line 441: err=''!obj'', msg=Unknown Error\n", "||PaMacCore (AUHAL)|| Error on line 1322: err='-10851', msg=Audio Unit: Invalid Property Value\n", "β Microphone test failed: Error opening InputStream: Internal PortAudio error [PaErrorCode -9986]\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Test your microphone first\n", "test_microphone_simple()" ] }, { "cell_type": "code", "execution_count": 30, "id": "843f15b3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "β Simple recording function created!\n", "Now try: record_5_seconds()\n" ] } ], "source": [ "# Simple 5-second recording function that works\n", "def record_5_seconds():\n", " \"\"\"Simple 5-second recording and analysis function\"\"\"\n", " print(\"π€ Starting 5-second musical note recording...\")\n", " \n", " try:\n", " # Import required libraries\n", " import sounddevice as sd\n", " import numpy as np\n", " from collections import Counter\n", " \n", " # Check if models exist\n", " try:\n", " trained_models_multi\n", " scaler_multi\n", " label_encoder\n", " extract_comprehensive_features\n", " except NameError as e:\n", " print(f\"β Missing required variable: {e}\")\n", " print(\"Please run all the training cells first!\")\n", " return None, None\n", " \n", " # Recording parameters\n", " duration = 5.0\n", " sample_rate = 22050\n", " \n", " print(f\"π΄ Recording for {duration} seconds...\")\n", " print(\"Start making sounds now!\")\n", " \n", " # Record audio\n", " recording = sd.rec(\n", " int(duration * sample_rate), \n", " samplerate=sample_rate, \n", " channels=1,\n", " dtype='float64'\n", " )\n", " \n", " # Countdown\n", " for i in range(5, 0, -1):\n", " print(f\"β° {i}...\")\n", " time.sleep(1)\n", " \n", " # Wait for recording to complete\n", " sd.wait()\n", " print(\"β Recording complete!\")\n", " \n", " # Check audio level\n", " audio_flat = recording.flatten()\n", " max_amplitude = np.max(np.abs(audio_flat))\n", " print(f\"Max amplitude: {max_amplitude:.4f}\")\n", " \n", " if max_amplitude < 0.001:\n", " print(\"β οΈ Very quiet audio. Try making louder sounds.\")\n", " \n", " # Play back recording\n", " print(\"\\nπ Playing back your recording:\")\n", " display(ipd.Audio(audio_flat, rate=sample_rate))\n", " \n", " # Extract features and predict\n", " print(\"\\nπ Analyzing audio...\")\n", " \n", " try:\n", " features = extract_comprehensive_features(audio_flat, sample_rate)\n", " features_scaled = scaler_multi.transform(features.reshape(1, -1))\n", " \n", " print(\"\\nπ― Model Predictions:\")\n", " print(\"-\" * 40)\n", " \n", " predictions = {}\n", " for name, model in trained_models_multi.items():\n", " pred_encoded = model.predict(features_scaled)[0]\n", " pred_note = label_encoder.inverse_transform([pred_encoded])[0]\n", " predictions[name] = pred_note\n", " \n", " # Get confidence if available\n", " if hasattr(model, 'predict_proba'):\n", " proba = model.predict_proba(features_scaled)[0]\n", " confidence = proba.max()\n", " print(f\"{name:<25}: {pred_note} ({confidence:.3f})\")\n", " else:\n", " print(f\"{name:<25}: {pred_note}\")\n", " \n", " # Ensemble result\n", " votes = list(predictions.values())\n", " vote_counts = Counter(votes)\n", " majority_vote = vote_counts.most_common(1)[0][0]\n", " vote_confidence = vote_counts.most_common(1)[0][1] / len(votes)\n", " \n", " print(f\"\\nπ FINAL RESULT:\")\n", " print(f\"Detected Note: {majority_vote}\")\n", " print(f\"Confidence: {vote_confidence:.1%}\")\n", " print(f\"Votes: {dict(vote_counts)}\")\n", " \n", " return majority_vote, predictions\n", " \n", " except Exception as e:\n", " print(f\"β Analysis failed: {e}\")\n", " return None, None\n", " \n", " except ImportError as e:\n", " print(f\"β Missing package: {e}\")\n", " print(\"Run: !pip install sounddevice\")\n", " return None, None\n", " except Exception as e:\n", " print(f\"β Recording failed: {e}\")\n", " print(\"Check your microphone permissions and connection.\")\n", " return None, None\n", "\n", "print(\"β Simple recording function created!\")\n", "print(\"Now try: record_5_seconds()\")" ] }, { "cell_type": "code", "execution_count": 31, "id": "3b3fd4cd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "π€ Starting 5-second musical note recording...\n", "π΄ Recording for 5.0 seconds...\n", "Start making sounds now!\n", "||PaMacCore (AUHAL)|| Warning on line 521: err=''!obj'', msg=Unknown Error\n", "||PaMacCore (AUHAL)|| Warning on line 441: err=''!obj'', msg=Unknown Error\n", "β Recording failed: Error opening InputStream: Internal PortAudio error [PaErrorCode -9986]\n", "Check your microphone permissions and connection.\n", "||PaMacCore (AUHAL)|| Error on line 1322: err='-10851', msg=Audio Unit: Invalid Property Value\n" ] }, { "data": { "text/plain": [ "(None, None)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "record_5_seconds()" ] }, { "cell_type": "code", "execution_count": null, "id": "43b94212", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "base", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }